package com.example.elm.Messages

import ChatAdapter
import android.os.Bundle
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.elm.R
import java.util.UUID


class Message_TalkActivity : AppCompatActivity() {

    private lateinit var recyclerView: RecyclerView
    private lateinit var messageInput: EditText
    private lateinit var sendtext: TextView
    private lateinit var backtext: TextView
    private lateinit var chatName: TextView
    private lateinit var firstName: TextView
    private lateinit var firstImage: ImageView
    private lateinit var firstMessage: TextView
    private var leftName: String? = null
    private var leftMessage: String? = null
    private var leftImage: Int = R.drawable.message_head
    val str: Array<String> = arrayOf("好的", "叮~，一张满20减2的商家代金券已经发放到您的账户，可与饿了么红包同享哦！", "【外卖已送达】您的外卖订单已送达指定地点，请尽快领取避免丢餐", "【进群领取专属优惠券】亲爱的顾客，感谢您下单，本店已开通商家顾客交流群，群中会经常发布优惠商品及营销活动，快来加入吧！")
    private lateinit var chatAdapter: ChatAdapter
    private val messageList = mutableListOf<MessageModel>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        leftName = intent.getStringExtra("left_name")
        leftMessage = intent.getStringExtra("left_message")
        leftImage = intent.getIntExtra("left_image", R.drawable.message_head)

        setContentView(R.layout.activity_message)

        recyclerView = findViewById(R.id.recyclerView)
        messageInput = findViewById(R.id.et_message)
        sendtext = findViewById(R.id.sendText)
        backtext = findViewById(R.id.backText)
        chatName = findViewById(R.id.ChatName)
        firstName = findViewById(R.id.firstleft_name)
        firstImage = findViewById(R.id.firstleft_image)
        firstMessage = findViewById(R.id.firstleft_content)

        chatName.text = leftName
        firstName.text = leftName
        firstImage.setImageResource(leftImage)
        firstMessage.text = leftMessage
        recyclerView.layoutManager = LinearLayoutManager(this)
        chatAdapter = ChatAdapter(messageList)
        recyclerView.adapter = chatAdapter
        sendtext.setOnClickListener {
            val messageText = messageInput.text.toString()
            if (messageText.isNotEmpty()) {
                sendMessage(messageText)
            }
        }
        backtext.setOnClickListener {
            finish()
        }
    }

    private fun sendMessage(content: String) {
        val randoms = (0..3).random()
        val newMessage = MessageModel(
            id = UUID.randomUUID().toString(),
            left_content = str[randoms],
            right_content = content,
            senderId = "lngwj",
            receiverId = leftName.toString(),
            receiverHead = leftImage
        )

        // 添加新消息到列表并更新 RecyclerView
        messageList.add(newMessage)
        chatAdapter.notifyItemInserted(messageList.size - 1)

        // 清空输入框
        messageInput.text.clear()

        // 滚动到底部
        recyclerView.scrollToPosition(messageList.size - 1)
    }
}

